﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Giant.Core.Domain.Users;
using Giant.Core.Domain.Media;
using Giant.Services.Common;
using Giant.Services.Users;
using Giant.Services.Helpers;
using Giant.Services.Localization;
using Giant.Services.Media;
using Giant.Services.Security;
using Giant.Services.Seo;
using Giant.Web.Framework;
using Giant.Web.Framework.Security;
using Giant.Web.Models.Common;
using Giant.Web.Models.Profile;

namespace Giant.Web.Controllers
{
    [HttpsRequirement(SslRequirement.No)]
    public partial class ProfileController : BasePublicController
    {
        private readonly ILocalizationService _localizationService;
        private readonly IPictureService _pictureService;
        private readonly IUserService _userService;
        private readonly IDateTimeHelper _dateTimeHelper;
        private readonly IPermissionService _permissionService;
        private readonly UserSettings _userSettings;
        private readonly MediaSettings _mediaSettings;

        public ProfileController(ILocalizationService localizationService,
            IPictureService pictureService,
            IUserService userService,
            IDateTimeHelper dateTimeHelper,
            IPermissionService permissionService,
            UserSettings userSettings,
            MediaSettings mediaSettings)
        {
            this._localizationService = localizationService;
            this._pictureService = pictureService;
            this._userService = userService;
            this._dateTimeHelper = dateTimeHelper;
            this._permissionService = permissionService;
            this._userSettings = userSettings;
            this._mediaSettings = mediaSettings;
        }

        public ActionResult Index(int? id, int? page)
        {
            if (!_userSettings.AllowViewingProfiles)
            {
                return RedirectToRoute("HomePage");
            }

            var userId = 0;
            if (id.HasValue)
            {
                userId = id.Value;
            }

            var user = _userService.GetUserById(userId);
            if (user == null || user.IsGuest())
            {
                return RedirectToRoute("HomePage");
            }

            bool pagingPosts = false;
            int postsPage = 0;

            if (page.HasValue)
            {
                postsPage = page.Value;
                pagingPosts = true;
            }

            var name = user.FormatUserName();
            var title = string.Format(_localizationService.GetResource("Profile.ProfileOf"), name);

            var model = new ProfileIndexModel
            {
                ProfileTitle = title,
                UserProfileId = user.Id
            };


            //display "edit" (manage) link
            if (_permissionService.Authorize(StandardPermissionProvider.AccessAdminPanel) && _permissionService.Authorize(StandardPermissionProvider.ManageUsers))
                DisplayEditLink(Url.Action("Edit", "User", new { id = user.Id, area = "Admin" }));


            return View(model);
        }

        //profile info tab
        [ChildActionOnly]
        public ActionResult Info(int userProfileId)
        {
            var user = _userService.GetUserById(userProfileId);
            if (user == null)
            {
                return RedirectToRoute("HomePage");
            }

            //avatar
            var avatarUrl = "";
            if (_userSettings.AllowUsersToUploadAvatars)
            {
                avatarUrl =_pictureService.GetPictureUrl(
                 user.GetAttribute<int>(SystemUserAttributeNames.AvatarPictureId),
                 _mediaSettings.AvatarPictureSize,
                 _userSettings.DefaultAvatarEnabled,
                 defaultPictureType: PictureType.Avatar);
            }
   
            //registration date
            bool joinDateEnabled = false;
            string joinDate = string.Empty;

            if (_userSettings.ShowUsersJoinDate)
            {
                joinDateEnabled = true;
                joinDate = _dateTimeHelper.ConvertToUserTime(user.CreatedOnUtc, DateTimeKind.Utc).ToString("f");
            }

            //birth date
            bool dateOfBirthEnabled = false;
            string dateOfBirth = string.Empty;
            if (_userSettings.DateOfBirthEnabled)
            {
                var dob = user.GetAttribute<DateTime?>(SystemUserAttributeNames.DateOfBirth);
                if (dob.HasValue)
                {
                    dateOfBirthEnabled = true;
                    dateOfBirth = dob.Value.ToString("D");
                }
            }

            var model = new ProfileInfoModel
            {
                UserProfileId = user.Id,
                AvatarUrl = avatarUrl,
                JoinDateEnabled = joinDateEnabled,
                JoinDate = joinDate,
                DateOfBirthEnabled = dateOfBirthEnabled,
                DateOfBirth = dateOfBirth,
            };

            return PartialView(model);
        }

       
    }
}
